package view;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import static java.lang.Math.*;

public class Line implements Picture{
	public static int NONE = 3;
	public static int TRUE = 1;
	public static int FALSE = 0;
	public static Point previous;
	public static int width = 8;
	private Block causeBlock;
	private Block secondBlock;
	private int ifStatu = NONE;
	private GeneralPath path;
	private Point secondLast;
	private Point last;
	
	private static final double pi=Math.PI;
	private static final int ARROW_LENGTH = 7;
	private static final double ARROW_ANGLE = Math.toRadians(30.0);

	
	public void draw(Graphics2D g) {
		g.setColor(Color.BLACK);
		g.draw(path);
		drawArrow(g, secondLast.x, secondLast.y, last.x, last.y);
	}
	public void drawRed(Graphics2D g) {
		g.setColor(Color.RED);
		g.draw(path);
		drawArrow(g, secondLast.x, secondLast.y, last.x, last.y);
	}
	public void cleanDraw(Graphics2D g){
		g.setColor(Color.WHITE);
		g.draw(path);
		drawArrow(g, secondLast.x, secondLast.y, last.x, last.y);
	}
	public boolean equals(Line line){
		if (line == this)
			return true;

		if (line == null)
			return false;
		return false;
	}
	public boolean contains(Point point){
		return path.contains(point);
	}
	public Block getCauseBlock() {
		return causeBlock;
	}
	public void setCauseBlock(Block causeBlock) {
		this.causeBlock = causeBlock;
	}
	public Block getSecondBlock() {
		return secondBlock;
	}
	public void setSecondBlock(Block secondBlock) {
		this.secondBlock = secondBlock;
	}
	public GeneralPath getPath() {
		return path;
	}
	public void setPath(GeneralPath path) {
		this.path = path;
	}
	public int getIfStatu() {
		return ifStatu;
	}
	public void setIfStatu(int ifStatu) {
		this.ifStatu = ifStatu;
	}
	public static Point calculatePoint(Point point, Point previous){
		int deltaX = abs(point.x - previous.x);
		int deltaY = abs(point.y - previous.y);
		if(deltaX <= width && point.y <= (previous.y - width)){//up spase
			return new Point(previous.x,point.y);
		}else if(deltaY <= width && point.x <= (previous.x - width)){//left spase
			return new Point(point.x, previous.y);
		}else if(deltaX <= width && point.y >= (previous.y + width)){//down space
			return new Point(previous.x, point.y);
		}else if(deltaY <= width && point.x >= (previous.x + width)){//rigth spase
			return new Point(point.x, previous.y);
		}
		return point;	
	}
	
	public static void drawSight(Graphics2D g, Point point){
		Stroke standart = g.getStroke();
		float param[] = {10.0f};
		BasicStroke newStrock = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,10.0f, param, 0.0f);
		g.setStroke(newStrock);
		//draw
		g.setColor(new Color(0.1F,0.5F,0.7F,0.5F));
		g.drawLine(point.x, point.y - 500, point.x, point.y + 500);
		g.drawLine(point.x - 800, point.y, point.x + 800, point.y);
		g.setColor(Color.BLACK);
		g.setStroke(standart);
	}
	public static void drawArrow(Graphics2D g, int x1, int y1, int x2, int y2) {
		int x3,y3,x4,y4;
		double angle = Math.atan2(y2-y1, x2-x1)+pi;
		x3=(int)(x2+Math.cos(angle-ARROW_ANGLE)*ARROW_LENGTH);
		y3=(int)(y2+Math.sin(angle-ARROW_ANGLE)*ARROW_LENGTH);
		x4=(int)(x2+Math.cos(angle+ARROW_ANGLE)*ARROW_LENGTH);
		y4=(int)(y2+Math.sin(angle+ARROW_ANGLE)*ARROW_LENGTH );
		Polygon polygon=new Polygon();
		polygon.addPoint(x2,y2);
		polygon.addPoint(x3,y3);
		polygon.addPoint(x4,y4);
		g.fillPolygon(polygon);
    }
	public Point getSecondLast() {
		return secondLast;
	}
	public void setSecondLast(Point secondLast) {
		this.secondLast = secondLast;
	}
	public Point getLast() {
		return last;
	}
	public void setLast(Point last) {
		this.last = last;
	}
}
